<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Phantom Wallet Connection</title>
  <script src="connectPhantom.js"></script>
</head>
<body>

<button id="connectWalletButton">连接 Phantom 钱包</button>
<div id="walletAddress"></div>

</body>
<script>
  document.getElementById('connectWalletButton').addEventListener('click', async () => {
    try {
      // 检查浏览器是否已安装Phantom钱包
      if (window.solana && window.solana.isPhantom) {
        // 尝试连接钱包
        const response = await window.solana.connect({ onlyIfTrusted: true }); // 使用 onlyIfTrusted 选项可以避免弹出不必要的连接请求
        // 显示钱包地址
        document.getElementById('walletAddress').innerText = `钱包地址: ${response.publicKey.toString()}`;
      } else {
        // 引导用户安装Phantom钱包
        document.getElementById('walletAddress').innerHTML = 'Phantom钱包未安装。请<a href="https://phantom.app/" target="_blank">点击此处</a>安装。';
      }
    } catch (err) {
      console.error(err);
      alert('连接钱包时发生错误。');
    }
  });

</script>
</html>
